/*弹出菜单*/
(function ($) {
    $.fn.yqPopMenu = function (opts, param) {
        if (typeof opts == 'string') {
            return $.fn.yqPopMenu.methods[opts](this, param);
        }

        return this.each(function () {
            var $this = $(this);
            var opt;
            if ($this.data("yqtype") == "yqPopMenu") {
                var $uniqueID = $this.attr("ID");
                var opts_=$.extend({}, options($this));
                destroy($this);
                $this = $("#" + $uniqueID);
                opt = $.extend(opts_, opts);
            }
            else {
                opt = $.extend({}, $.fn.yqPopMenu.defaults, opts);
                var $uniqueID = $this.attr("ID");
                if ($uniqueID === undefined || $uniqueID === null || $uniqueID == "") {
                    $uniqueID = $.getUniqueID();
                    $this.attr("ID", $uniqueID);
                }
            }

            //变量信息
            $this.data("popmenu_opt", opt);
            $this.data("yqtype", "yqPopMenu");
 
            inittopmenu($this);
        });
    };

    $.fn.yqPopMenu.defaults = {
        width: 150,
        menus: [],
        bind: null,
        beforeBindingTitle: function (menus, callback) {
            callback(menus);
        }, //标题绑定前
        afterBindingTitle: function () { }, //标题绑定后
        beforeOpen: function (callback) {
            callback(true)
        },
    };

    $.fn.yqPopMenu.methods = {
        options: function (jq) {
            return options($(jq[0]));
        },
        destroy: function (jq) {
            destroy($(jq[0]));
        },
        getBtn:function(jq, id){
            return getBtn($(jq[0]), id);
        },
        enable: function (jq, id) {
            isEdit($(jq[0]), id, true);
        },
        disable: function (jq, id) {
            isEdit($(jq[0]), id, false);
        },
        show: function (jq, id) {
            isShow($(jq[0]), id, true);
        },
        hide: function (jq, id) {
            isShow($(jq[0]), id, false);
        },
        bind: function (jq, $obj) {
            bindPop($(jq[0]), $obj);
        },
        open: function (jq, e) {
            showPop($(jq[0]), e);
        }
    };

    function options($object) {
        return $object.data("popmenu_opt");
    }

    function form_load($object) {
        var opt = options($object);

        if (!(opt.bind === null) && opt.bind.length > 0) {
            bindPop($object, opt.bind);
        }

        opt.afterBindingTitle.call($object);
    }

    function destroy($object) {
        var opt = options($object);
        if(opt===undefined){
            return;
        }

        if(typeof opt.menu!="undefined"){
            opt.menu.menu("destroy");
        }
        
        $object.removeData("topmenu_opt");
        $object.removeData("yqtype");
        $object.empty();
    }

    function inittopmenu($object) {
        var opt = options($object);

        opt.beforeBindingTitle.call($object, opt.menus, function (menus) {
            opt.menus = menus;
            initmenu_root($object, menus);
        });
    }

    function initmenu_root($object, menus) {
        var opt = options($object);
        var $uniqueID = $object.attr("id");

        initmenu_subs($uniqueID, opt.width, opt.menus,
            function ($div) {
                opt.menu = $div;

                $object.after($div);
                $div.attr("id", $uniqueID + "_popmenu")
                $div.menu({});

                form_load($object);
            });
    }

    function initmenu_subs($uniqueID, width, menus, callback) {
        var $div = $('<div style="width:' + width + 'px;">');

        for (var i = 0; i < menus.length; i++) {
            if (menus[i].subs === undefined) {
                menus[i].subs = [];
            }

            var $obj = $('<div></div>');
            $div.append($obj);

            if(!(menus[i].data === undefined)){
                $obj.data("extdata", menus[i].data);
            }

            if (menus[i].text === undefined && menus[i].iconCls === undefined) {
                $obj.addClass("menu-sep");
            }
            else {
                if (menus[i].subs == 0) {
                    if (typeof menus[i].text != "undefined" && menus[i].text.length > 0) {
                        $obj.html(menus[i].text);
                    }
                    if (typeof menus[i].iconCls != "undefined" && menus[i].iconCls.length > 0) {
                        $obj.attr("data-options", "iconCls:'" + menus[i].iconCls + "'");
                    }

                    $obj.attr("id", $uniqueID + "_" + menus[i].id + "_pop");

                    if (menus[i].onClick) {
                        $obj.data("onClick", menus[i].onClick);
                        if(typeof menus[i].options!="undefined"){
                            $obj.data("menu-opt", menus[i].options);
                        }
                        $obj.bind('click', function () {
                            $(this).data("onClick").call(this);
                        });
                    }
                }
                else {
                    var $span = $('<span>Open</span>');
                    if (typeof menus[i].text != "undefined" && menus[i].text.length > 0) {
                        $span.html(menus[i].text);
                    }
                    if (typeof menus[i].iconCls != "undefined" && menus[i].iconCls.length > 0) {
                        $span.attr("data-options", menus[i].iconCls);
                    }
                    $obj.append($span);
                    initmenu_subs($uniqueID,
                        (menus[i].width === undefined ? 150 : menus[i].width), menus[i].subs,
                        function ($s_div) {
                            $obj.append($s_div);
                        });
                }
            }
        }

        callback($div);
    }

    function getBtn($object, id) {
        var opt = options($object);
        var $uniqueID = $object.attr("id");
        var $btn = $("#" + $uniqueID + "_" + id + "_pop");
        if ($btn.length == 0) {
            return null;
        }
        return $btn;
    }

    function isEdit($object, id, tf) {
        var opt = options($object);
        var $uniqueID = $object.attr("id");
        var $btn = $("#" + $uniqueID + "_" + id + "_pop");
        if ($btn.length == 0) {
            return;
        }

        if (!tf) {
            $btn.attr("disabled", "disabled").css({ "cursor": "default", "opacity": "0.4" });
        }
        else {
            $btn.removeAttr("disabled").css({ "cursor": "pointer", "opacity": "1" });
        }
    }

    function isShow($object, id, tf) {
        var opt = options($object);
        var $uniqueID = $object.attr("id");
        var $btn = $("#" + $uniqueID + "_" + id + "_pop");
        if ($btn.length == 0) {
            return;
        }
        if (tf) {
            $btn.show();
        } else {
            $btn.hide();
        }
    }

    //绑定到组件上
    function bindPop($object, $obj) {
        $obj.bind('contextmenu', function (e) {
            e.preventDefault();
            showPop($object, e);
        });
    }

    //显示
    function showPop($object, e) {
        var opt = options($object);
        var $uniqueID = $object.attr("id");

        opt.beforeOpen.call($object, function (tf) {
            if (tf) {
                $("#" + $uniqueID + "_popmenu").menu('show', {
                    left: e.pageX,
                    top: e.pageY
                });
            }
        })
    }
})(jQuery);